﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

/*
 * bing搜索引擎url
 * https://cn.bing.com/search?q=(key
 * 百度搜索引擎url
 * https://www.baidu.com/s?wd=(key
*/

namespace Homework_10th
{
    /// <summary>
    /// 采用委托的方式，在其他线程中使用主界面线程才能用到的修改界面元素的方法
    /// </summary>
    public delegate void SearchEngineHandler();

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 点击确定，启动两个线程，
        /// 分别进行包括百度搜索的委托和包括bing搜索的委托
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            new Thread(startSearchBing).Start();
            //textBoxBing.Text = "搜索中。。。";
            new Thread(startSearchBaidu).Start();
            //textBoxBaidu.Text = "搜索中。。。";
        }

        /// <summary>
        /// 以委托的方式调用bing搜索的方法
        /// </summary>
        private void startSearchBing()
        {
            SearchEngineHandler handler = new SearchEngineHandler(searchBing);
            try
            {
                this.Invoke(handler);
            }
            catch (Exception) { }
        }
        /// <summary>
        /// 调用GetWebPage类的方法，使用bing进行搜索，并处理返回的网页内容
        /// </summary>
        private void searchBing()
        {
            textBoxBing.Text = "搜索中。。。\r\n。。。";
            string urlBing = "https://cn.bing.com/search?q=" + textBoxKey.Text;
            try
            {
                string page = GetWebPage.getPage(urlBing);
                //string content= page;
                //string content= GetWebPage.getCharaContent(page);
                string content = GetWebPage.getChineseCharactor(page);
                textBoxBing.Text = "";
                if (!textBoxKey.Text.Equals(content.Substring(0, textBoxKey.Text.Length)))
                    textBoxBing.Text = textBoxKey.Text + "\r\n";
                textBoxBing.Text += content;
            }
            catch (ReadException re)
            {
                textBoxBing.Text = re.ToString();
            }
        }

        /// <summary>
        /// 以委托的方式调用百度搜索的方法
        /// </summary>
        private void startSearchBaidu()
        {
            SearchEngineHandler handler = new SearchEngineHandler(searchBaidu);
            try
            {
                this.Invoke(handler);
            }
            catch (Exception) { }
        }
        /// <summary>
        /// 调用GetWebPage类的方法，使用百度进行搜索，并处理返回的网页内容
        /// </summary>
        /// 


        private void searchBaidu()
        {
            textBoxBaidu.Text = "搜索中。。。\r\n。。。";
            string urlBaidu = "https://www.baidu.com/s?wd=" + textBoxKey.Text;
            try
            {
                string page = GetWebPage.getWebPageUsingCookie(urlBaidu);
                //string content=textBoxBaidu.Text = page;
                //string content= GetWebPage.getCharaContent(page);
                string content= GetWebPage.getChineseCharactor(page);
                textBoxBaidu.Text = "";
                if (!textBoxKey.Text.Equals(content.Substring(0, textBoxKey.Text.Length)))
                    textBoxBaidu.Text = textBoxKey.Text + "\r\n";
                textBoxBaidu.Text += content;
            }
            catch (ReadException re)
            {
                textBoxBaidu.Text = re.ToString();
            }
        }
    }
}
